package com.arong.swing.db.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.arong.swing.db.JDBCUtils;
import com.arong.swing.db.entity.AsAllGenerate;
import com.arong.swing.exception.AppException;
import com.arong.swing.exception.SysException;
import com.arong.swing.util.StringUtil;
public class AsAllGenerateSDAO  extends BaseDAO {
	public AsAllGenerateSDAO(){
		this(DB_ANRONGTEC);
	}
	
	public AsAllGenerateSDAO(String dbFile){
		super(dbFile);
	}
	public AsAllGenerateSDAO(JDBCUtils jdbcUtils){
		super(jdbcUtils);
	}
	public String getCreateTableSql(){
		return "CREATE TABLE IF NOT EXISTS "+
				"AS_ALL_GENERATE("+
				  "ID  VARCHAR2(32) PRIMARY KEY,"+
				  "TIME DATE NOT NULL,"+
				  "VER VARCHAR2(32) NOT NULL,"+
				  "FILE_PATH VARCHAR2(256) NOT NULL"+
				")";
		
	}
	
	/**
	 * 插入
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int insert(AsAllGenerate entity) throws SysException,AppException {
		int i=0;
		try {
		 		    if (StringUtil.isBlank(entity.getId())) {
				entity.setId(getSeqValue());
			}
		 			i= jdbcUtils.insert(entity);
		} catch (SQLException e) {
			throw new SysException("add error", e);
		}
		return i;
	}

	/**
	 * 删除
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  int delete(AsAllGenerate entity) throws SysException,AppException {
		int i = 0;
		try {
			i=jdbcUtils.execute("DELETE FROM AS_ALL_GENERATE WHERE ID='"
					    +entity.getId()
		    		    +"'"
			);
		} catch (SQLException e) {
			throw new SysException("delete error", e);
		}
		return i;
	}
	public  int deleteAll() throws SysException,AppException {
		int i = 0;
		try {
			i=jdbcUtils.execute("DELETE FROM AS_ALL_GENERATE "
			);
		} catch (SQLException e) {
			throw new SysException("delete error", e);
		}
		return i;
	}
	/**
	 * 查询 集合
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public   List<AsAllGenerate> queryList(AsAllGenerate entity) throws SysException,AppException {
	    List<AsAllGenerate> res=null;
	    try {
			res= jdbcUtils.queryList(getSql(entity), AsAllGenerate.class,
			getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryList集合查询 error",e);
		}
		return   res;
	}

	/**
	 * 查询对象
	 * @param entity
	 * @return
	 * @throws SysException, AppException
	 */
	public  AsAllGenerate queryBean(AsAllGenerate entity) throws SysException,AppException {
		AsAllGenerate res=null;
	    try {
			res=  jdbcUtils.queryBean(getSql(entity), AsAllGenerate.class,
				getParams(entity));
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}
	public  AsAllGenerate getLatest() throws SysException,AppException {
		AsAllGenerate res=null;
	    try {
	    	StringBuffer sql = new StringBuffer("SELECT ");
			sql.append(" a.ID,a.TIME,a.VER,a.FILE_PATH");
			sql.append(" from AS_ALL_GENERATE a ORDER BY TIME desc LIMIT 0,1");
			res=  jdbcUtils.queryBean(sql.toString(), AsAllGenerate.class);
		} catch (SQLException e) {
			throw new SysException("queryBean对象查询 error",e);
		}
		return   res;
	}
	/**
	 * 获取查询条件
	 * @return
	 */
	private Object[] getParams(AsAllGenerate asAllGenerate) {
		List<Object> qryList = new ArrayList<Object>();
		if (asAllGenerate.getId() != null) {
			qryList.add(asAllGenerate.getId());
		}
		if (asAllGenerate.getTime() != null) {
			qryList.add(asAllGenerate.getTime());
		}
		if (asAllGenerate.getVer() != null) {
			qryList.add(asAllGenerate.getVer());
		}
		if (asAllGenerate.getFilePath() != null) {
			qryList.add(asAllGenerate.getFilePath());
		}
		return jdbcUtils.setQryList(qryList);
	}

	/**
	 * 获取sql
	 * @return
	 */
	private String getSql(AsAllGenerate asAllGenerate) {
		StringBuffer sql = new StringBuffer("SELECT ");
		sql.append(" a.ID,a.TIME,a.VER,a.FILE_PATH");
		sql.append(" from AS_ALL_GENERATE a where 1=1");
		if (asAllGenerate.getId() != null) {
			sql.append(" and ID=?");
		}
		if (asAllGenerate.getTime() != null) {
			sql.append(" and TIME=?");
		}
		if (asAllGenerate.getVer() != null) {
			sql.append(" and VER=?");
		}
		if (asAllGenerate.getFilePath() != null) {
			sql.append(" and FILE_PATH=?");
		}
		return sql.toString();
	}
}

